package com.example.springspidemo.processor;

import com.example.springspidemo.service.MessageService;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

/**
 * @Author: ZhangGongMing
 * @CreateTime: 2025/4/15 12:53
 * @Description:
 * @Version: 1.0
 */
public class MessageServicePostProcessor implements BeanPostProcessor {

    /**
     * 只需要改前置操作
     *
     * @param bean the new bean instance
     * @param beanName the name of the bean
     * @return
     * @throws BeansException
     */
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof MessageService) {
            return new MessageService() {
                @Override
                public String getMessage() {
                    return ((MessageService) bean).getMessage() + "--postProcessor By Spring SPI";
                }
            };
        }
        return bean;
    }


    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
    }


}
